package com.jzqf.harmonydemo.base;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;

import java.util.ArrayList;
import java.util.List;

/**
 * 列表适配器基类
 *
 * @author LiuWeiHao
 * 2021/4/30 16:43
 */
public abstract class BaseProvider<T> extends BaseItemProvider {
    protected AbilitySlice mAbilitySlice;
    protected List<T> mList;
    private ItemChildClickedListener itemChildClickedListener;

    public BaseProvider(AbilitySlice abilitySlice) {
        this.mAbilitySlice = abilitySlice;
        mList = new ArrayList<>();
    }

    public BaseProvider(AbilitySlice abilitySlice, List<T> list) {
        this.mAbilitySlice = abilitySlice;
        this.mList = list;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public T getItem(int position) {
        if (mList != null && position >= 0 && position < mList.size()) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        Component componentItem;
        BaseViewHolder holder;
        if (component == null) {
            componentItem = LayoutScatter.getInstance(mAbilitySlice).parse(getLayoutId(), null, false);
            holder = new BaseViewHolder(componentItem);
            componentItem.setTag(holder);
        } else {
            componentItem = component;
            holder = (BaseViewHolder) componentItem.getTag();
        }
        holder.setItemClickedListener(componentId -> {
            if (itemChildClickedListener != null) {
                itemChildClickedListener.itemChildClick(componentId, position);
            }
        });
        bindData(holder, position);
        return componentItem;
    }

    /**
     * Item 布局Id
     *
     * @return LayoutId
     */
    protected abstract int getLayoutId();

    /**
     * 绑定数据
     *
     * @param holder   BaseViewHolder
     * @param position item索引
     */
    protected abstract void bindData(BaseViewHolder holder, int position);

    public void notifyData(List<T> list) {
        if (list != null) {
            mList.clear();
            mList.addAll(list);
            //TODO 没法刷新
            notifyDataChanged();
        } else {
            mList.clear();
            notifyDataChanged();
        }
    }

    public void append(List<T> response) {
        if (response != null) {
            mList.addAll(response);
        }
        notifyDataChanged();
    }

    /**
     * 根据索引移除对象
     *
     * @param index 索引
     */
    public void remove(int index) {
        mList.remove(index);
        notifyDataSetItemRemoved(index);
    }

    /**
     * 移除指定对象
     *
     * @param object 对象
     */
    public void remove(T object) {
        mList.remove(object);
        notifyDataChanged();
    }

    /**
     * 清空列表
     */
    public void clear() {
        mList.clear();
        notifyDataChanged();
    }

    public void setItemChildClickedListener(ItemChildClickedListener itemChildClickedListener) {
        this.itemChildClickedListener = itemChildClickedListener;
    }

    public interface ItemChildClickedListener {
        void itemChildClick(int componentId, int position);
    }
}
